// pages/share_post_new/share_post_new.js
import * as canvas from '../../utils/canvas.js';
const app = getApp(), core = require('../../utils/core');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picData: [
      // 'https://xilu-images.oss-cn-shanghai.aliyuncs.com/xianpin/share_img/8_share.jpg',
      // 'https://xilu-images.oss-cn-shanghai.aliyuncs.com/xianpin/share_img/3_share.jpg'
    ],
    picTemp: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   text1: options.text1, text2: options.text2, text3: options.text3, text4: options.text4,
    //   picData: [options.img1, options.img2]
    // });
    let data = core.getCache('temp');
    if(data) {
      this.setData(data);
      this.setData({
        picData:[
          data.img2, // 二维码
          data.img1||'https://xilu-images.oss-cn-shanghai.aliyuncs.com/assets/img/avatar.png' //头像||logo
        ]});
    }
    this.onCreateDownload();
  },

  onCreateDownload(){
    var that = this;
    // 网络图片下载到本地再生成图片
    var pic_path = that.data.picData;
    wx.showLoading({title: '生成中...'});
    // for (var i = 0; i < pic_path.length; i++) {
    //   canvas.downloadFile(pic_path[i]).then((path) => {
    //     that.data.picTemp.push(path);
    //     if(pic_path.length == that.data.picTemp.length) {
    //       that.canvasToDraw();
    //     }
    //   }, () => {
    //     wx.showModal({
    //       title: '提示',
    //       content: '生成图片失败',
    //     })
    //   });
    // }
    wx.downloadFile({
      url: pic_path[0],
      success: function (res) {
        that.data.picTemp.push(res.tempFilePath);
        wx.downloadFile({
          url: pic_path[1],
          success: function (ress) {
            that.data.picTemp.push(ress.tempFilePath);
            that.canvasToDraw();
          },
          fail: function (error) {
            wx.showModal({
              title: '提示',
              content: '生成图片2失败:'+error,
            })
          },
        });
      },
      fail: function (err) {
        wx.showModal({
          title: '提示',
          content: '生成图片1失败:'+err,
        })
      },
    });
  },

  canvasToDraw: function () {
    let that = this;
    let ctx = wx.createCanvasContext('mayCanvas');
    ctx.setFillStyle('#fff')
    ctx.fillRect(0,0,750,1334);
    ctx.save();

    // 绘制背景图
    var bgPath = '/images/img_post-bg.png';
    canvas.clipRoundImg(ctx,0,0,750,1334,0,bgPath);
    // 绘制二维码
    var codePath = that.data.picTemp[0];
    canvas.clipCircleRect(ctx,225,340,150,'#dee3ff');
    canvas.clipCircleImg(ctx,233,348,142,codePath);
 
    // 绘制白色背景块
    canvas.clipRoundRect(ctx,30,1028,690,245,10);
    // 绘制缩略图
    var bgPath = that.data.picTemp[1];
    canvas.clipRoundImg(ctx,50,1048,147,147,10,bgPath);
    // 绘制标题
    ctx.setFillStyle("#000");
    var title = this.data.text1;
    canvas.fillTextCut(ctx,title,26,26,227,1055,470,1);
    // 绘制简介
    ctx.setFillStyle("#737373");
    var info = this.data.text2;
    canvas.fillTextCut(ctx,info,22,22,227,1095,470,2);
    var info1 = this.data.text3;
    canvas.fillTextCut(ctx,info1,22,22,227,1132,470,2);
    var info2 = this.data.text4;
    canvas.fillTextCut(ctx,info2,22,22,227,1175,470,1);
    // 绘制提示
    ctx.setFillStyle("#000");
    var tip = this.data.tip;
    canvas.fillTextCut(ctx,tip,20,20,30,1235,690,1,true);

    ctx.save();
    ctx.draw(false, function (e) {
      that.canvasToFile();
    })
  },
  canvasToFile: function () {
    var that = this;
    setTimeout(() => {
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: 750,
        height: 1334,
        destWidth: 750,
        destHeight: 1334,
        canvasId: 'mayCanvas',
        success: function (res) {
          wx.hideLoading();
          that.setData({filePaths: res.tempFilePath});
        }
      })
    }, 300);
  },
  canvasToSaveImage: function () {
    var that = this;
    //图片保存到本地
    var filePaths = this.data.filePaths;
    wx.showLoading({title: '保存中...'});
    if(!filePaths){
      setTimeout(function(){
        that.canvasToSaveImage();
      },500)
      return false;
    }
    wx.hideLoading();
    wx.saveImageToPhotosAlbum({
      filePath: filePaths,
      success: function (res) {
        wx.showToast({
          title: '保存成功',
        })
      },
      fail: function (res) {
      }
    })
  },







})
